package com.lark.wifi.dialog;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.cdl.library.config.DialogConfig;
import com.mtk.launcher.R;
import com.lark.wifi.base.IWifi;
import com.lark.wifi.base.WifiManager;
import com.mtk.launcher.databinding.DialogTipsBinding;
import com.mtk.launcher.util.shared.SharedPerManager;

import java.util.Locale;

public class TipWifiDialog extends Dialog implements View.OnClickListener {

    DialogTipsBinding mView;
    private OnButtonListener buttonListener;
    private IWifi data;
    private int mX;
    Context context;

    public TipWifiDialog(@NonNull Context context) {
        super(context, R.style.CustomDialogTheme);
        this.context = context;
    }


    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mView = DialogTipsBinding.inflate(getLayoutInflater(), null, false);
        setContentView(mView.getRoot());
        mView.tvDisconnect.setOnClickListener(this);
        mView.tvCancelSave.setOnClickListener(this);
        mView.tvNegative.setOnClickListener(this);
        if (data == null) {
            return;
        }
        mView.dialogTitle.setText(data.name());
        mView.tvStatus.setText(getContext().getString(R.string.connected));
        String singleStr = WifiManager.signalLevToStr(context, data.level());
        mView.tvSignal.setText(singleStr);
        mView.tvPway.setText(data.encryption());
        mView.tvIpaddr.setText(data.ip());
        mView.tvMac.setText(data.getMacAddress().toUpperCase(Locale.ROOT));
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = SharedPerManager.getScreenWidth() / 2;
        params.height = ActionBar.LayoutParams.WRAP_CONTENT;
    }

    public TipWifiDialog setParamX(int mX) {
        this.mX = mX;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (buttonListener != null) {
            buttonListener.onButton(v.getId());
        }
        dismiss();
    }

    public TipWifiDialog setData(IWifi data) {
        this.data = data;
        return this;
    }

    public TipWifiDialog setCenterListener(OnButtonListener listener) {
        this.buttonListener = listener;
        return this;
    }

    public interface OnButtonListener {
        void onButton(int buttonId);
    }

//    @Override
//    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
//        MyLog.keyCode("setOnKeyChangeListener=" + keyCode);
//        if (keyCode == DialogConfig.KEYCODE_ACTION_UP) {
//            KeyControl.getInstance().sendMessageTo(KeyEvent.KEYCODE_DPAD_LEFT);
//            return true;
//        }
//        if (keyCode == DialogConfig.KEYCODE_ACTION_DOWN) {
//            KeyControl.getInstance().sendMessageTo(KeyEvent.KEYCODE_DPAD_RIGHT);
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
}
